﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Widgetsphere.Generator.Common.GeneratorFramework;
using Widgetsphere.Generator.Models;
using Widgetsphere.Generator.ProjectItemGenerators;
using Widgetsphere.Generator.DataTransfer;

namespace Acme.GeneratorExtension.OverrideDTO
{
	/// <summary>
	/// This generator will generate the [EntityName]MetaData.cs file. This file 
	/// contains a public partial class [EntityName]MetaData. For the generated
	/// file the class will be empty. This file is provided as a way to allow programmers to
	/// extend the generated class. It will be generated once and never over written after that 
	/// point.
	/// </summary>
	[GeneratorItemAttribute("MetaData Class Extension", typeof(DataTransferObjectGeneratedGenerator))]
	public class OverrideDTOGenerator : BaseGenerator
	{
		#region Class Members

		private const string RELATIVE_OUTPUT_LOCATION = @"\";

		#endregion

		#region Overrides

		public override int FileCount
		{
			get { return _model.Database.Tables.Count; }
		}

		public override void Generate()
		{
			foreach (Table table in (from x in _model.Database.Tables where x.Generated orderby x.Name select x))
			{
				if (table.Generated)
				{
					OverrideDTOTemplate template = new OverrideDTOTemplate(_model, table);
					string fullParentName = RELATIVE_OUTPUT_LOCATION + template.ParentItemName;
					ProjectItemGeneratedEventArgs eventArgs = new ProjectItemGeneratedEventArgs(template.FileName, template.FileContent, ProjectName, fullParentName, this, true);
					OnProjectItemGenerated(this, eventArgs);
				}
			}
			ProjectItemGenerationCompleteEventArgs gcEventArgs = new ProjectItemGenerationCompleteEventArgs(this);
			OnGenerationComplete(this, gcEventArgs);

		}

		protected override string ProjectName
		{
			get
			{
				ModelRoot modelRoot = ((ModelRoot)_model);
				return string.Format("{1}.{0}.DataTransfer", modelRoot.ProjectName, modelRoot.CompanyName);
			}
		}

		#endregion


	}
}